Skip to content

Sub state vector#8077

Merged
NoureldinYosri merged 4 commits into
quantumlib:mainfrom
AntonKast:sub_state_vector
May 15, 2026
Merged

Sub state vector#8077
NoureldinYosri merged 4 commits into
quantumlib:mainfrom
AntonKast:sub_state_vector

Conversation

@AntonKast
Copy link
Copy Markdown
Contributor

The current implementation appears to be exponential in space and time. The present change, due originally to jamieas@google.com, replaces python loops with numpy functions motivated by standard methods for computing partial traces. The change is runnable in Colab, including tests and a performance benchmark, at

https://colab.sandbox.google.com/drive/1qWJTnlT6hPXI8cQsTwyqQveEtn7fGhNc?resourcekey=0-67OmbvgwTlf9fnKp3ppbrw

The current implementation appears to be exponential in space and time. The
present change, due originally to jamieas@google.com, replaces python loops
with numpy functions motivated by standard methods for computing partial
traces. The change is runnable in Colab, including tests and a performance
benchmark, at

  https://colab.sandbox.google.com/drive/1qWJTnlT6hPXI8cQsTwyqQveEtn7fGhNc?resourcekey=0-67OmbvgwTlf9fnKp3ppbrw
@AntonKast AntonKast requested a review from a team as a code owner May 15, 2026 04:16
@AntonKast AntonKast requested a review from dabacon May 15, 2026 04:16
@google-cla
Copy link
Copy Markdown

google-cla Bot commented May 15, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@github-actions github-actions Bot added the size: M 50< lines changed <250 label May 15, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented May 15, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.63%. Comparing base (76910aa) to head (cdb3216).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #8077   +/-   ##
=======================================
  Coverage   99.63%   99.63%           
=======================================
  Files        1110     1110           
  Lines      100067   100074    +7     
=======================================
+ Hits        99705    99712    +7     
  Misses        362      362           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@AntonKast
Copy link
Copy Markdown
Contributor Author

CLA check is fixed.

Copy link
Copy Markdown
Collaborator

@NoureldinYosri NoureldinYosri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

overall LGTM%nits

num_qubits_out = len(keep_indices)#.shape[0]

# The permutation moves the specified qubits to the start of the qubit order.
keeps = set(keep_indices)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: frozenset

if protocols.approx_eq(coherence_measure, 1, atol=atol):
return np.exp(2j * np.pi * np.random.random()) * best_candidate.reshape(ret_shape)
# The number of output qubits.
# keep_indices = np.array(keep_indices)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# keep_indices = np.array(keep_indices)

return np.exp(2j * np.pi * np.random.random()) * best_candidate.reshape(ret_shape)
# The number of output qubits.
# keep_indices = np.array(keep_indices)
num_qubits_out = len(keep_indices)#.shape[0]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
num_qubits_out = len(keep_indices)#.shape[0]
num_qubits_out = len(keep_indices)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Fixed.

Copy link
Copy Markdown
Collaborator

@NoureldinYosri NoureldinYosri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @AntonKast

@NoureldinYosri NoureldinYosri added this pull request to the merge queue May 15, 2026
Merged via the queue into quantumlib:main with commit 02e927f May 15, 2026
44 checks passed
pull Bot pushed a commit to soloinovator/Cirq that referenced this pull request May 15, 2026
- Verify the maximum eigenvalue is indeed close to 1.

- Enable arbitrary order of `keep_indices` as before.

- Use the same tolerance check as before quantumlib#8077 with rtol=0.
  This allows to restore previous tests instead of having
  to do them with larger state vector deviation.

- Put back RaiseValueErrorIfNotProvided as a default for
`sub_state_vector`.
  This allows using `None` for the fallback return value.
    
- Add check for negative values in `keep_indices`.

Follow-up to quantumlib#8077
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size: M 50< lines changed <250

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants